<!DOCTYPE html>
<html height100 xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta th:replace="authority/common/currency/head"/>
    <style>
        .search-container {
            min-height: 20px;
        }

        .page-body .table-item {
            margin-bottom: 20px;
        }

        .page-body .table-item:last-child {
            margin-bottom: 10px;
        }
    </style>
</head>

<body class="childrenBody" padding10>
<div class="page-body">

</div>

<script type="text/html" id="page_temp">
    <div>
        <div class="search-container"></div>
        <table class="table-container" lay-filter="dynamic-table"></table>
    </div>
</script>

<script type="text/html" id="sys_toolbarDemo">
    <button class="layui-btn layui-btn-sm" lay-event="sys_toolbar_search">搜索</button>
    <button class="layui-btn layui-btn-sm" lay-event="sys_toolbar_refresh">
        <i class="layui-icon" style="line-height:30px">&#xe669;</i>
    </button>
</script>
<script th:replace="authority/common/currency/footer"></script>
<script type="text/javascript" th:src="@{/authority/js/commonUtils.js}"></script>
<script type="text/javascript" th:inline="javascript">
    layui.extend({
        complexSearch: 'complexSearch/complexSearch', // {/}的意思即代表采用自有路径，即不跟随 base 路径
        initTable: 'initTable/initTable'
    });
    layui.use(['initTable', 'complexSearch', 'lutil'], function () {
        var table = layui.table
            , complexSearch = layui.complexSearch
            , layer = layui.layer
            , lutil = layui.lutil;
        var page_temp_html = $('#page_temp').html();
        var defaultUrlParam = getUrlParamMap(location.href), defaultParam = [], defaultWhereParam = {};
        $.each(defaultUrlParam, function (i, e) {
            if (e === undefined) return true;
            if (i === 'pageId' || i === 'tableId') return true;
            if (i === 'searchParams') {
                try {
                    var searchParams = JSON.parse(e);
                    if (!searchParams['formula'] || searchParams['formula'] === '')
                        searchParams['formula'] = '=';
                    $.extend(defaultParam, searchParams);
                } catch (e) {
                    console.log("查询参数解析错误：" + e);
                }
                return true;
            }
            defaultParam.push({field: i, formula: '=', value: e});
            defaultWhereParam[i] = e;
        });
        /*<![CDATA[*/
        var sys_config = JSON.parse((/*[[${config}]]*/'') || '[]');

        /* ]]>*/
        function reckonTableHeight(id) {
            var height = $(window).height() - $('#' + id).outerHeight() - 35;
            return parseInt(height) > 300 ? parseInt(height) : 300;
        }

        $.each(sys_config, function (i, e) {
            var table_config = e['table_config'], search_config = e['search_config'],
                toolbar_config = e['toolbar_config'];
            var table_temp = $(page_temp_html)
                , tableElemId = table_config['id'] + "-table", tableId = table_config['id'],
                searchElemId = table_config['id'] + "-search"
                , $search = table_temp.find('div.search-container').attr("id", searchElemId)
                ,
                $table = table_temp.find('table.table-container').attr("id", tableElemId).attr("lay-filter", tableElemId);
            $('div.page-body').append($('<div class="table-item"></div>').append($search).append($table));
            // 搜索框
            var searchFun = search_config['config']['search'] ? eval("(" + search_config['config']['search'] + ")") : undefined;
            search_config['config']['search'] && delete search_config['config']['search'];
            var search = complexSearch.render($.extend({
                container: "#" + searchElemId,
                isDynamic: false,
                submit: false,
                returnFormat: {
                    dataTypeName: 'type'
                },
                search: function (data) {
                    var thisTable = layui.pageObtainTable(tableId);
                    data = $.extend([], defaultParam, data);
                    searchFun ? searchFun(thisTable, data) : thisTable && thisTable.query({searchs: data.length > 0 ? JSON.stringify(data) : ''});
                }
            }, search_config['config']));
            search.setDefaultValues(defaultParam);
            // toolbar
            $.each(toolbar_config, function (i, e) {
                $('body').append("<script type='text/html' id='" + e['id'] + "'>" + e['content'] + "<\/script>");
                if (e['event'])
                    try {
                        e['event'] = eval('(' + e['event'] + ')');
                    } catch (e) {
                        delete e['event'];
                    }
            });

            // 表格
            $.each(table_config['cols'][0], function (i, e) {
                if (e['templet']) e['templet'] = eval("(" + e['templet'] + ")");
            });
            var _csrf_headerName = /*[[${_csrf.headerName}]]*/'', _csrf_token = /*[[${_csrf.token}]]*/'';
            layui.page_table_examples.push(table.render($.extend(table_config, {
                headers: {[_csrf_headerName]: _csrf_token}
                , where: $.extend({searchs: JSON.stringify(defaultParam)}, table_config['where'], defaultWhereParam)
                , height: reckonTableHeight(searchElemId)
                , done: table_config['done'] && eval("(" + table_config['done'] + ")")
            })));

            table.on('sort(' + tableElemId + ')', function (obj) {
                var thisTable = layui.pageObtainTable(tableId);
                thisTable && thisTable.sort(obj, function (where) {
                    where['sorts'] = JSON.stringify([{field: obj.field, order: obj.type || ''}]);
                });
            });

            //头工具栏事件
            table.on('toolbar(' + tableElemId + ')', function (obj) {
                var thisTable = layui.pageObtainTable(tableId);
                var checks = table.checkStatus(obj.config.id);
                switch (obj.event) {
                    case 'sys_toolbar_refresh':
                        thisTable && thisTable.refresh();
                        break;
                    case  'sys_toolbar_search':
                        search.submit();
                        break;
                }
            });

            table.on('tool(' + tableElemId + ')', function (obj) {
                var thisTable = layui.pageObtainTable(tableId);
                var data = obj.data; //获得当前行数据
                var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
                $.each(toolbar_config, function (i, e) {
                    var events = e['content'].match(/lay-event='(.+?)'|lay-event="(.+?)"|lay-event = "(.+?)"|lay-event = '(.+?)'/g);
                    if (events != null) {
                        $.each(events, function (i1, e1) {
                            if (layEvent === e1.replace(/\s+/g, '').replace('lay-event=', '').replace(/["']/g, ''))
                                return (e['event'] && e['event'](layEvent, thisTable, data)), true;
                        })
                    }
                })
            });
        });
    })
</script>
</body>
</html>